package day11;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XMLCreate {
    public static void main(String[] args) {
      add("web前端高级课程","珠海出版社","h5 c3,es6",66.7,4,1,29);
        //modify(4,"html5,css3,es6等前端的知识","前端高级");
        //del("java高级特性");
    }
    public static void del(String name){
        SAXReader reader=new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\courses.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        //name右重复，下面的代码有错
        List<Element> list = doc.selectNodes("/books/book[name='" + name + "']");
        if(list.size()==0){
            System.out.println("没有");
            return;
        }
        for(Element node:list){
            node.getParent().remove(node);
        }


        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\courses.xml"),format);
            xw.write(doc);
            xw.close();
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void modify(int no,String desc,String name){
        SAXReader reader=new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\courses.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        Element node =(Element) doc.selectSingleNode("/books/book[@no=" + no + "]");
        if(node==null){
            System.out.println("没有");
            return;
        }
        node.selectSingleNode("name").setText(name);
        node.selectSingleNode("description").setText(desc);

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\courses.xml"),format);
            xw.write(doc);
            xw.close();
            System.out.println("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //增加一个book节点
    public static void add(String name,String publish,String desc,double price,int no,int diff,int coursetime){
        SAXReader reader=new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\ke.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
            //return;
            doc= DocumentHelper.createDocument();
            doc.addElement("books");
        }
        Element root = doc.getRootElement();
        Element newBook = root.addElement("book");
        newBook.addAttribute("publish",publish);
        newBook.addAttribute("no",String.valueOf(no));
        Element ename = newBook.addElement("name");
        ename.setText(name);
        Element eprice = newBook.addElement("price");
        eprice.setText(String.valueOf(price));

        Element ediff = newBook.addElement("difficulty");
        ediff.setText(String.valueOf(diff));

        Element etime = newBook.addElement("coursetime");
        etime.setText(String.valueOf(coursetime));
        Element edesc = newBook.addElement("description");
        edesc.setText(desc);

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\ke.xml"),format);
            xw.write(doc);
            xw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
